<template>
  <div class="table-page-search-wrapper ccslh">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="6" :sm="12">
            <a-form-item label="基地名称">
              <a-input v-model="queryParam.lhpyjdmc"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="基地编号">
              <a-input v-model="queryParam.jdbh"/>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="院系">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.yxm" @change="yxChange">
                <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="专业类别">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.zym" >
                <a-select-option :key="i.id" :value="i.id" v-for="i in selectMenu.zy">{{ i.xkdm+'-'+i.xkmc  }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :sm="{span:12}" :lg="{span:12}" :xl="{span:6}" >
            <a-form-item label="基地级别">
              <a-select :allowClear="true" showSearch optionFilterProp="children" v-model="queryParam.jdjb" >
                <a-select-option v-for="i in this.JDJB" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :md="6" :sm="12">
            <a-form-item label="合同">
              <a-select :allowClear="true" v-model="queryParam.sfht" >
                <a-select-option value="1">是</a-select-option>
                <a-select-option value="0">否</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="单位地址">
              <a-input v-model="queryParam.dwdz"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="基地联系人">
              <a-input v-model="queryParam.jdlxr"/>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="审核状态">
              <a-select :allowClear="true" v-model="queryParam.shzt">
              <a-select-option v-for="i in this.TYPESHZT" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="12">
            <a-form-item label="是否过期">
              <a-select :allowClear="true" v-model="queryParam.htzt">
                <a-select-option value="01">生效中</a-select-option>
                <a-select-option value="02">已过期</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="$refs.table.refresh(true)" style="margin:0 8px">查询</a-button>
              <a-button type="primary" @click="batchDelete" v-hasPermission="'lhpyjd:delete'">批量删除</a-button>
              <a-button type="primary" @click="showCheck" v-hasPermission="'lhpyjd:plsh'" style="margin:0 8px">批量审核</a-button>
              <a-button type="primary" @click="shtjFn()" v-hasPermission="'lhpyjd:tjsh'">批量提交</a-button>
              <a-button type="primary" @click="exportExcel">导出</a-button>
            </span>
          </a-col>
        </a-row>
<!--        <a-row style="margin-bottom: 20px;">
          <a-upload style="display:inline-block" name="file" :multiple="true" action="https://www.mocky.io/v2/5cc8019d300000980a055e76" @change="handleChange">
            <a-button>
              <a-icon type="upload" /> 点击选择导入文件
            </a-button>
          </a-upload>
          <a-button type="primary">导入</a-button>
        </a-row>-->
      </a-form>
    </a-card>

    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        v-if="initDone"
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pageSize="parameter.pageSize"
        :pageNum="parameter.pageNum"
        :scroll="{ x: 1680 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 + (parameter.pageNo - 1) * parameter.pageSize }}
        </span>
        <span slot="sfht" slot-scope="text">
          {{ text === '0' ? '否' : (text === '1' ? '是' : '') }}
        </span>
        <span slot="psffrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <span slot="pszzrq" slot-scope="a">{{a && a.slice(0,11)}}</span>
        <a slot="dsbh" slot-scope="a, b" @click="direct(b)">{{a}}</a>
        <template slot="operation" slot-scope="a,b">
          <template >
              <a @click="toEdit(b)" v-hasPermission="'lhpyjd:editAfterSubimt'">修改</a>
              <a-divider type="vertical" v-hasPermission="'lhpyjd:editAfterSubimt'" />
          </template>
          <template  v-if="b.shzt != '02' && b.shzt != '03'">
              <a @click="showCheck(b)" v-hasPermission="'lhpyjd:plsh'">审核</a>
              <a-divider v-hasPermission="'lhpyjd:plsh'" type="vertical" />
          </template>
          <a @click="toLook(b)">查看</a>

          <a @click="shtjFn(b)" v-if="b.shzt == '0'" v-hasPermission="'lhpyjd:tjsh'">提交</a>
        </template>
      </s-table>
    </a-card>

    <check-modal
          ref="checkModal"
          :visible.sync="visible"
          @close="() => { visible = false }"
          @save="batchCheck"
        />
  </div>
</template>

<script>
  import {STable} from '~'
  import {mapState} from 'vuex'
  import checkModal from './component/checkModal'

  export default {
  data () {
    return {
      visible: false,
      checkId:"", //单个审核选中ID
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: '序号',
          width: 100,
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: '基地名称',
          dataIndex: 'lhpyjdmc',
          width: 200
        },
        {
          title: '基地编号',
          dataIndex: 'jdbh',
          width: 100,
          scopedSlots: { customRender: 'jdbh' }
        },
        {
          title: '依托学院',
          dataIndex: 'yxmc',
          width: 150
        },
        {
          title: '专业类别',
          dataIndex: 'zylbmc',
          width: 150
        },
        {
          title: '基地级别',
          dataIndex: 'jdjbmc',
          width: 150
        },
        {
          title: '合同',
          dataIndex: 'sfht',
          width: 80,
          scopedSlots: { customRender: 'sfht' }
        },
        {
          title: '合同期限',
          dataIndex: 'htjssj',
          width: 150
        },
        {
          title: '是否过期',
          dataIndex: 'sfgq',
          width:160
        },
        {
          title: '单位地址',
          dataIndex: 'dwdz',
          width: 250
        },
        {
          title: '基地联系人',
          dataIndex: 'jdlxr',
          width: 100
        },
        {
          title: '备注',
          dataIndex: 'bz',
          width: 400
        },
        {
          title: '审核状态',
          dataIndex: 'shztmc',
          width: 100,
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' },
          width: 180,
          fixed: 'right'
        }
      ],
      parameter: {
        pageNo: 1,
        pageSize: 10
      },
      loadData: parameter => {
        this.parameter = parameter
        this.$refs.table && this.$refs.table.clearSelected()
        parameter.pageNum = parameter.pageNo
        let params = Object.assign({},this.queryParam, parameter)
        this.$DateToString(params)
        return this.$api.baseTeacher.listLhpyjd(params).then(res => {
          console.log(res.data)
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {},
      teacherInfo: {},
      initDone: false,
      yx:[],
      zy:[],
    }
  },
  components: {STable, checkModal},
  computed: {
    ...mapState({
      JDJB: state => state.app['JDJB'], // 导师类别
      TYPESHZT: state => state.app['TYPESHZT'], // 导师状态
    })
  },
  beforeRouteEnter (to, from, next) {
    console.log(from)
    next(vm => {
      let pageParams = vm.$store.state.app.pageParams || {}
      if ((from.name === '培养基地管理' || from.name === '基地信息修改' ) && pageParams.path === vm.$route.path) {
        vm.queryParam = pageParams.params
        vm.parameter = pageParams.pagination
      }
      vm.initDone = true
    })
  },
  beforeRouteLeave (to, from, next) {
    if (to.name === '培养基地管理' || to.name === '基地信息修改' ) {
      this.$store.dispatch('app/setPageParams', {
        path: this.$route.path,
        params: this.queryParam,
        pagination: this.parameter
      })
    }
    next()
  },
  created () {
    this.initSelectMenu()
    this.$store.dispatch('app/setDictionary', 'TYPESHZT')
    this.$store.dispatch('app/setDictionary', 'JDJB')
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll(),
        this.$api.baseTeacher.listDegree(),

      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data,
          zy:res[1].data
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    showCheck (data) {
      console.log(data.id)
      if(data.id){
        this.checkId = data.id
        console.log(this.checkId)
        this.visible = true
      }else{
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要审核的基地')
          return
        }
        var flag =false
        this.selectedRows.forEach(item => {
          if(item.shzt != '01'){
            flag = true
            return
          }
        })
        if(flag){
           this.$message.warning('不能选择已审核的数据')
            return
        }
        this.checkId = ""
        this.visible = true
      }
    },
    batchCheck (params) {
      this.$refs.checkModal.confirmLoading = true
      let dsbhs = []
      if(this.checkId){
        //单个审核
          dsbhs = [this.checkId]
      }else{
        //多选审核
         dsbhs = this.selectedRows.map(ds => {
          return ds.id
        })
        console.log(dsbhs)
      }
      this.$api.baseTeacher.lhpyjdCheck({ids: dsbhs,...params}).then(() => {
        this.$message.success('操作成功')
        this.selectedRowKeys = []
        this.selectedRows = []
        this.visible = false
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
        this.$refs.checkModal.form.resetFields()
      }).finally(() => {
        this.$refs.checkModal.confirmLoading = false
      })
    },
    shtjFn(data){
      if (!this.selectedRowKeys.length && !data) {
          this.$message.warning('请选择需要提交审核的基地')
          return;
      }
      this.$refs.checkModal.confirmLoading = true
      let dsbhs = []
      if(data && data.id){
        //单个提交
          dsbhs = [data.id]
      }else{
        //多选提交
         dsbhs = this.selectedRows.map(ds => {

          return ds.id
        })
      }
      this.$api.baseTeacher.lhpyjdShtj(Object.assign({ids: dsbhs})).then(() => {
        this.$message.success('操作成功')
        this.selectedRowKeys = []
        this.selectedRows = []
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
      }).finally((e) => {
        console.log(e)
        this.$refs.checkModal.confirmLoading = false
      })
    },
    yxChange (code) {
      this.$set(this.queryParam, 'zy', '')
      if (code) {
        this.$api.base.majorAll({lsdwh: code}).then(res => {
          this.$set(this.selectMenu, 'zy', res.data)
        })
      } else {
        this.$set(this.selectMenu, 'zy', [])
      }
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toEdit (data) {
      this.$router.push(`/degree/trainingBase/base/${data.id}/edit`)
    },
    toLook (data) {
      this.$router.push(`/degree/trainingBase/base/${data.id}/edit?look=true`)
    },
    toChange(data){
      let title = ''
      if(!data.zszt || data.zszt === '0'){
        title = '确定打开导师招生状态吗?'
      }else{
        title = '确定关闭导师招生状态吗?'
      }
      this.$confirm({
        title: '操作提示',
        content: title,
        centered: true,
        onOk: () => {
          this.$api.teacher.zsztChage(data.id).then(() => {
            this.$message.success('修改成功')
            this.$refs.table.refresh(false)
          })
        },
        onCancel: () => {
        }
      })
    },
    batchDelete () {
      if (!this.selectedRowKeys.length) {
        this.$message.warning('请选择需要删除的基地')
        return
      }
      this.$confirm({
        title: '确定删除所选中的基地信息?',
        content: '当您点击确定按钮后，这些基地信息将会被彻底删除',
        centered: true,
        onOk: () => {
          let baseIds = []
          this.selectedRows.forEach(base => {
            baseIds.push(base.id)
          })
          this.$api.baseTeacher.deleteLhpyjd(baseIds.join(',')).then(() => {
            this.$message.success('删除成功')
            this.selectedRowKeys = []
            this.selectedRows = []
            this.$refs.table.refresh(false)
            this.$refs.table.clearSelected()
          })
        },
        onCancel: () => {
        }
      })
    },
    issueContract (param) {
      let dsbh = this.selectedRows.map(t => t.dsbh)
      let params = Object.assign({dsbh}, param)
      this.$DateToString(params)
      this.$api.teacher.issueContract(params).then(res => {
        this.contractVisible = false
        this.$message.success(res.msg)
        this.$refs.table.refresh(false)
        this.$refs.table.clearSelected()
      })
    },
    handleChange (info) {
      console.log(info)
    },
    exportExcel () {
      let params = this.queryParam
      this.$export('/base/lhpyjd/export', {
        ...params
      })
    }
  }
}
</script>

<style lang="less">
.ccslh{
      table {
        table-layout:fixed;
      }
      tr td:nth-child(12),tr td:nth-child(3){
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
      .ant-table-fixed-header .ant-table-scroll .ant-table-header{
        width: calc(100% - 8px);//减去滚动条的宽度
      }
}

</style>
